package com.preskool.controller;

import com.preskool.dto.ActionResult;
import com.preskool.po.Sports;
import com.preskool.service.SportsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: lihao
 * @email: 2470849570@qq.com
 * @date: 20212021/1/7
 * @describe: 运动项目控制层
 * @version: v1.0
 */
@Slf4j
@RestController
public class SportController {

    @Autowired //注入sportService
    private SportsService sportsService;

    @RequestMapping(value = "/sports",method = RequestMethod.GET)
    public ActionResult findAllSports(@RequestParam(defaultValue = "1") Integer currentPage,@RequestParam(defaultValue = "5") Integer pageSize,String sortName){
        List<Sports> allSports = sportsService.findAllSports(currentPage, pageSize,sortName);
        for (Sports sports:allSports){
            log.info("*****"+sports.toString()+"******");
        }
        return new ActionResult(200,"查询成功",allSports) ;
    }

    @RequestMapping(value = "/addSport",method = RequestMethod.PUT)
    public ActionResult insertSport(@RequestBody Sports sports){
        int i = sportsService.addSport(sports);
        if (i>0){
            return new ActionResult(200,"插入信息成功",i);
        }else{
            return new ActionResult(400,"插入数据失败",null);
        }
    }

    /**
     * 根据前台传输的spid进行删除数据
     * @param id
     * @return
     */
    @RequestMapping(value = "/deleteSport",method = RequestMethod.DELETE)
    public ActionResult deleteSportById(Integer id){
        int i = sportsService.deleteSport(id);
        if (i>0){
            return new ActionResult(200,"删除成功",null);
        }else{
            return new ActionResult(403,"删除操作失败",null);
        }
    }

    /**
     * 更新操作
     * @param sports
     * @return
     */
    @RequestMapping(value = "/updateSport",method = RequestMethod.POST)
    public ActionResult updateSport(@RequestBody Sports sports){
        int i = sportsService.updateSport(sports);
        if (i>0){
            return new ActionResult(200,"更新成功成功",null);
        }else{
            return new ActionResult(403,"更新操作失败",null);
        }
    }
}
